package com.colors.diners.controller;

import com.colors.commons.model.domain.ResultInfo;
import com.colors.commons.model.dto.DinersDTO;
import com.colors.commons.model.vo.ShortDinerInfo;
import com.colors.commons.utils.ResultInfoUtil;
import com.colors.diners.service.DinersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 食客控制层
 *
 * @author colors
 * @date 2021/4/29 17:10
 */
@RestController
public class DinersController {

    @Resource
    private DinersService dinersService;
    @Resource
    private HttpServletRequest httpServletRequest;



    /**
     * 根据 ids 查询食客信息
     *
     * @param ids
     * @return
     */
    @GetMapping("findByIds")
    public ResultInfo<List<ShortDinerInfo>> findByIds(String ids) {
        List<ShortDinerInfo> dinerInfos = dinersService.findByIds(ids);
        return ResultInfoUtil.buildSuccess(httpServletRequest.getServletPath(), dinerInfos);
    }

    /**
     * 注册
     *
     * @param dinersDTO
     * @return
     */
    @PostMapping("register")
    public ResultInfo register(@RequestBody DinersDTO dinersDTO) {
        return dinersService.register(dinersDTO, httpServletRequest.getServletPath());
    }

    /**
     * 登录
     *
     * @param account
     * @param password
     * @return
     */
    @GetMapping("login")
    public ResultInfo login(@RequestParam String account, @RequestParam String password) {
        return dinersService.signIn(account, password, httpServletRequest.getServletPath());
    }

    /**
     * 检查手机号是否注册
     *
     * @param phone
     * @return
     */
    @GetMapping("checkPhone")
    public ResultInfo checkPhoneIsRegistered(String phone) {
        dinersService.checkPhoneIsRegistered(phone);
        return ResultInfoUtil.buildSuccess(httpServletRequest.getServletPath());
    }

}
